﻿var oldContent = '';

function onChangePasswordComplete(content) {
    oldContent = $("#passForm").html();
    reloadChangePassowrdTabs();
    //<img src='" + changePasswd_icon + "' alt='note' align='middle' />&nbsp;
    $("#passForm").html("<span class='close'>" + content.Message + "</span>");
}

function reloadContent() {
    reset();
    if (oldContent != '') {
        reloadChangePassowrdTabs();
        $("#passForm").html(oldContent);
    }
}

function reloadChangePassowrdTabs() {

    //Na kliknięcie 'Moje dane'
    $('.t_click').each(function (i, elem) {
        if (i != 2) {
            //Dla zakładek różnych od zmiana hasła:
            $(this).removeClass('t_active');
        }
        else {
            //Dla zakładki zmień hasło:
            $('.t_table').hide();
        }
    });

    $('.t_table').hide();
    $('#t_table_2').show();
}


function hidePasswordBox(e) {
    if ((e.type == 'keydown' && e.which == 27) || (e.type == 'click')) {
        if (!$('#passwordBox').parent().is('body'))
            $('#passwordBox').parent().hide();
    }
}
function reset() {
    $('input[type="text"][name="Phone"]').val($('input[type="hidden"][name="Phone"]').val());
    $('input[type="text"][name="PhoneRepeat"]').val($('input[type="hidden"][name="PhoneRepeat"]').val());

    $('input[type="text"][name="Email"]').val($('input[type="hidden"][name="Email"]').val());
    $('input[type="text"][name="PrivateEmail"]').val($('input[type="hidden"][name="PrivateEmail"]').val());

    $('input[type="password"][name="CurrentPassword"]').val($('input[type="hidden"][name="CurrentPassword"]').val());
    $('input[type="password"][name="NewPassword"]').val($('input[type="hidden"][name="NewPassword"]').val());
    $('input[type="password"][name="RepeatedNewPassword"]').val($('input[type="hidden"][name="RepeatedNewPassword"]').val());
    $('input[type="password"][name="PasswordHint"]').val($('input[type="hidden"][name="PasswordHint"]').val());
    $('#sms_row').html('');
    $('#SMSCode').val('');
    $('#SmsCodeRow').hide();
}

function popupChangePhone(data) {
    setTimeout('popupChangePhoneTimeout(\'' + data+ '\')', 2000);
}

function popupChangePhoneTimeout(data) {
    $('a#FancyBoxPasswordLink').trigger('click');
    $('.t_click:eq(0)').trigger('click');
    $('#sms_row').html("<span class='sms_phone'>" + data + "</span>");
}

////Sprawdzenie poprawności pól telefon i powtórz telefon i wysyłanie żądania o kod SMS
$('#RequestSmsCode').live('click', function () {
    if ($('#PhoneRepeat').hasClass('valid')) {
        $.post('/Kafeteria/' + lang + '/Employee/RequestSMSCode', { 'phoneNumber': $('#PhoneRepeat').val() }, function (data) {
            if (data.status == 'ok') {
                //Lokalne zapisanie kodu z SMS
                $('#RequestSmsCodeHdnf').val(data.code.toString());
                $('#SmsCodeRow').show();
            }
            else {
                alert(data.code);
            }
        });
    }
});

////Prawdziwa zmiana telefonu
$('#ChangePhone').live('click', function () {
    if ($('#PhoneRepeat').hasClass('valid')) {
        //Jeżeli lokalny kod SMS = kod z pola tekstowego
        if (($('#RequestSmsCodeHdnf').val() == $('#SMSCode').val())) {
            $.post('/Kafeteria/' + lang + '/Employee/ChangePhoneAjaxian', { 'phoneNumber': $('#PhoneRepeat').val() }, function (data) {
                alert(data.status);
                $('#cp_close').trigger('click');
                /* Zapisanie hiddenfielda telefonu i powtórz telefon aby funkcja reset() dobrze wpisała wartości */
                $('input[type="hidden"][name="Phone"]').val($('#PhoneRepeat').val());
                $('input[type="hidden"][name="PhoneRepeat"]').val($('#PhoneRepeat').val());
            });
        } else {
            alert(SmsCodeNotMatch);
        }
    }
});

//Blokowanie copy/paste
$(document).ready(function () {
    $('#t_table_0 * input[type="text"]').keydown(function (event) {
        if (event.ctrlKey == true && (event.which == '118' || event.which == '86' || event.which == '67' || event.which == '99' || event.which == '120' || event.which == '88')) {
            alert(CopyPasteInfo);
            event.preventDefault();
        }
    });

    //    //Blokowanie PPM na oknie zmiany hasła/nr telefonu
    $('#passForm').bind('contextmenu', function (event) {
        alert(RightMouseClickInfo);
        event.preventDefault();
    });

    $('#t_change_phone, #t_change_email').hide();

    $('.t_click').live('click', function (e) {
        $('#sms_row').html('');
        //Na każdej zakładce usuwamy styl aktywny
        $('.t_click').removeClass('t_active');

        $('.t_table').hide();

        //Pokazujemy odpowiednią zakładkę
        var ind = $(this).find('input:hidden').val();
        $('#t_table_' + ind).show();

        if (ind == "0") {
            $('#t_save input').not('.close').hide();
        }
        else {
            $('#t_save input').not('.close').show();
        }
        //Kliknięcie anuluj i ustawienie starych wartości pól formularza.
        $('.resetpasswordform').live('click', function () {
            setTimeout(reset, 1000);
        });

        $('#t_save').hide();
        $('#t_save').show();

        //$(e.currentTarget).css({ 'border': '1px solid #fff' });
    });

    //Zmiana kolorku napisu walidacji
    $('.validation_text').css({ 'color': '#2B2B2B' });

    $('#FancyBoxPasswordLink').click(function () {
        $('#passwordBox').show();
        reset();
        $("#hidden_link_change_password").fancybox({
            'width': 550,
            'height': 270,
            'overlayColor': '#fff',
            'autoDimensions': false,
            'autoScale': false,
            'transitionIn': 'none',
            'transitionOut': 'none'
        }).trigger('click');
    });

    //I trzeba w brzydki sposób oprogramować znikanie passwordboxa z fancybox...
    $('body').live('keydown', hidePasswordBox);
    $('a#fancybox-close').live('click', hidePasswordBox);
    $('.do-btns-start').live('click', hidePasswordBox);
    $('#cp_close').live('click', function(e){$.fancybox.close();});

});


